package com.tro.mail.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.tro.common.util.file.FileUtils;
import com.tro.common.util.office.OfficeUtil;
import com.tro.common.util.string.StringUtlis;
import com.tro.mail.dao.entity.MailMain;
import com.tro.mail.service.MailMainManager;

/**
 * 
* @ClassName: WritController
* @Description: TODO(写信)
* @author leeko
* @date 2017年10月27日 下午4:37:45
*
 */
public class WritController {
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@RequestMapping(value = "addPhoto")
	public String addPhoto() {
		return "/mail/imageUpload";
	}

	@RequestMapping(value = "addDoc")
	public String addDoc() {
		return "/mail/docUpload";
	}

	@RequestMapping(value = "addAnnex")
	public String addAnnex() {
		return "/mail/annexUpload";
	}

	@RequestMapping("uploadPicture")
	@ResponseBody
	public List<Map<String, String>> uploads(@RequestParam("file") MultipartFile[] files, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			return FileUtils.uploads(files, "", request.getSession().getServletContext().getRealPath(""));
		} catch (Exception e) {
			logger.error("上传失败", e);
		}
		List<Map<String, String>> error = new ArrayList<>();
		Map<String, String> re = new HashMap<>();
		re.put("isSuccess", "0");
		error.add(re);
		return error;
	}

	@RequestMapping("uploadDoc")
	@ResponseBody
	public List<Map<String, String>> uploadDoc(@RequestParam("file") MultipartFile[] files, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			List<Map<String, String>> result = FileUtils.uploads(files, "/static/doc",
					request.getSession().getServletContext().getRealPath(""));
			if (result != null && result.size() > 0) {
				String basePath = request.getSession().getServletContext().getRealPath("") + "/static/doc/";
				// 获取文件名
				String fileName = result.get(0).get("fileName");
				String imgPath = request.getSession().getServletContext().getRealPath("") + "/static/doc/image/";
				String content = OfficeUtil.toHtml(fileName, basePath, imgPath);
				content = StringUtlis.cleanHtml(content);
				result.get(0).put("fileUrl", content);
				result.get(0).put("isSuccess", "1");
			}
			return result;
		} catch (Exception e) {
			logger.error("上传失败", e);
		}
		List<Map<String, String>> error = new ArrayList<>();
		Map<String, String> re = new HashMap<>();
		re.put("isSuccess", "0");
		error.add(re);
		return error;
	}
	@Autowired
	MailMainManager mailManager;
	@RequestMapping(value = "save")
	public String save(MailMain mail, HttpServletRequest request) {
		// 避免依赖，直接从SESSION获取
		String createUserId = request.getSession().getAttribute("MAIL_USER_ID").toString();
		String createUserName = request.getSession().getAttribute("MAIL_USER_NAME").toString();
		mail.setCreateTime(new Date());
		mail.setCreateUserId(createUserId);
		mail.setCreateUserName(createUserName);
		mailManager.sendMailN(mail);
		return "/mail/Tips";
	}
}
